using System;
using System.Data;
using System.Data.SqlClient;

namespace MessageBoard
{
	/// <summary>
	/// The Forum Groups
	/// </summary>
	public class Group : Connection
	{
		#region Fields
		private int _Id;
		private string _Title;
		private string _Description;
		private Boolean _WithCategories;
		private User _AddedBy;
		private DateTime _AddedOn;
		#endregion

		#region Constructors
		/// <summary>
		/// The Group constructor
		/// </summary>
		/// <example>
		/// Group group = new Group();
		/// </example>
		public Group()
		{
			_Title = "";
			_Description = "";
		}
		#endregion

		#region Functions
		/// <summary>
		/// return the group stored in the DataRow.
		/// </summary>
		/// <param name="dr"></param>
		/// <returns></returns>
		public static Group Get(System.Data.DataRow dr)
		{
			Group group = new Group();
			group.Id = int.Parse(dr["Id"].ToString());
			group.Title = dr["Title"].ToString();
			group.Description = dr["Description"].ToString();
			group.WithCategories = Convert.ToBoolean(dr["WithCategories"].ToString());
			group.AddedBy = User.Get(int.Parse(dr["AddedBy"].ToString()));
			group.AddedOn = Convert.ToDateTime(dr["AddedOn"].ToString());
			return group;
		}

		/// <summary>
		/// Get the Group by its Id.
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		/// <example>
		/// Group group = Group.Get(groupId);
		/// </example>
		public static Group Get(int id)
		{
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter("SELECT * FROM " + Connection.tblGroup + " WHERE [Id] = @Id", DbConnection);
			getData.SelectCommand.Parameters.Add("@Id", id);
			getData.Fill(ds);
			if(ds.Tables[0].Rows.Count>0)
			{
				return Get(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// Get all the Groups.
		/// </summary>
		/// <returns></returns>
		/// <example>
		/// foreach(Group group in Group.GetAll())
		/// {
		///		Response.Write(group.Title);
		/// }
		/// </example>
		public static System.Collections.ArrayList GetAll()
		{
			System.Collections.ArrayList arrayList = new System.Collections.ArrayList();
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter("SELECT * FROM " + Connection.tblGroup + "", DbConnection);
			getData.Fill(ds);
			foreach(System.Data.DataRow dr in ds.Tables[0].Rows)
			{
				arrayList.Add(Get(dr));
			}
			return arrayList;
		}		
		#endregion

		#region Methods
		public void Insert()
		{
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter(@"
			INSERT INTO " + Connection.tblGroup + @" 
			([Title], [Description], [WithCategories], [AddedBy], [AddedOn])
			VALUES
			(@Title, @Description, @WithCategories, @AddedBy, @AddedOn);
			SELECT @@identity as i", DbConnection);
			getData.SelectCommand.Parameters.Add("@Title", _Title);
			getData.SelectCommand.Parameters.Add("@Description", _Description);
			getData.SelectCommand.Parameters.Add("@WithCategories", _WithCategories);
			getData.SelectCommand.Parameters.Add("@AddedBy", _AddedBy.Id);
			getData.SelectCommand.Parameters.Add("@AddedOn", _AddedOn);
			
			getData.Fill(ds);
			if(ds.Tables[0].Rows.Count>0)
			{
				_Id = int.Parse(ds.Tables[0].Rows[0]["i"].ToString());
			}
		}

		public void Update()
		{
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter(@"
			UPDATE " + Connection.tblGroup + @" SET 
			[Title] = @Title,
			[Description] = @Description,
			[WithCategories] = @WithCategories,
			[AddedBy] = @AddedBy,
			[AddedOn] = @AddedOn
			WHERE [Id] = @Id", DbConnection);
			getData.SelectCommand.Parameters.Add("@Title", _Title);
			getData.SelectCommand.Parameters.Add("@Description", _Description);
			getData.SelectCommand.Parameters.Add("@WithCategories", _WithCategories);
			getData.SelectCommand.Parameters.Add("@AddedBy", _AddedBy.Id);
			getData.SelectCommand.Parameters.Add("@AddedOn", _AddedOn);
			
			getData.SelectCommand.Parameters.Add("@Id", _Id);
			getData.Fill(ds);
		}

		public void Delete()
		{
			// Delete the related categories first.
			DeleteCategories();

			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter(@"
			DELETE FROM " + Connection.tblGroup + @" WHERE [Id] = @Id", DbConnection);
			getData.SelectCommand.Parameters.Add("@Id", _Id);
			getData.Fill(ds);
		}
		

		/// <summary>
		/// Delete the related categories.
		/// </summary>
		private void DeleteCategories()
		{
			foreach(Category category in Category.GetAll(this))
			{
				category.Delete();
			}
		}
		#endregion

		#region Properties
		
		/// <summary>
		/// The Id of the group.
		/// </summary>
		public int Id
		{
			get
			{
				return _Id;
			}
			set
			{
				_Id = value;
			}
		}

		/// <summary>
		/// The Title of the group, maximum length = 100
		/// </summary>
		public string Title
		{
			get
			{
				return _Title;
			}
			set
			{
				_Title = value;
			}
		}

		/// <summary>
		/// the Description of the group, maximum length = 255
		/// </summary>
		public string Description
		{
			get
			{
				return _Description;
			}
			set
			{
				_Description = value;
			}
		}

		/// <summary>
		/// set or get if this group have categories or not.
		/// </summary>
		public Boolean WithCategories
		{
			get
			{
				return _WithCategories;
			}
			set
			{
				_WithCategories = value;
			}
		}

		/// <summary>
		/// The user who add this group.
		/// </summary>
		public User AddedBy
		{
			get
			{
				return _AddedBy;
			}
			set
			{
				_AddedBy = value;
			}
		}

		/// <summary>
		/// The date this group is added.
		/// </summary>
		public DateTime AddedOn
		{
			get
			{
				return _AddedOn;
			}
			set
			{
				_AddedOn = value;
			}
		}
		#endregion
	}
}
